package com.bnz.service.impl;


import com.bnz.mapper.ClassesMapper;
import com.bnz.mapper.StudentMapper;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;

    // 1. 查询所有学生
    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectAll();
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        return students;
    }
    // 2. 分页查询
    @Override
    public PageResult<Student> findByPage(int page, int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Student> students = findAll();

        if (students.size() > 0) {
            System.out.println("学生数据不为空");
        }
        // 指定学生所在的班级
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //转换为page对象
        Page<Student> studentPage = (Page<Student>) students;
        // 返回分页结果
        return new PageResult<>(studentPage.getTotal(), studentPage.getResult());
    }
}
